package com.codeanalysissys.backend.entity;

import com.codeanalysissys.backend.entity.DTO.AnalysisResultDTO;
import com.codeanalysissys.backend.graphs.AbstractProgramGraph;
import lombok.Data;

import java.io.File;
import java.util.ArrayList;

@Data
public class UploadFile {
    public static final int JSONL = 0;
    public static final int SINGLE = 1;

    private String fileId;
    private String name;
    private int type;
    private Language language;
    private String filePath;
    private String desc;
    private int state;
    private String graphPath;
    private String dataPath;
    private ArrayList<AbstractProgramGraph> graphData;

    public UploadFile() {

    }

    public UploadFile(Builder builder) {
        this.fileId = builder.fileId;
        this.name = builder.name;
        this.type = builder.type;
        this.language = builder.language;
        this.filePath = builder.filePath;
        this.desc = builder.desc;
        this.state = builder.state;
        this.graphPath = builder.graphPath;
        this.dataPath = builder.dataPath;
        this.graphData = builder.graphData;
    }

    public AnalysisResultDTO toAnalysisResultDTO() {
        AnalysisResultDTO analysisResultDTO = new AnalysisResultDTO().builder().fileId(fileId).name(name)
                .type(type).lang(language != null ? language.getName() : null).state(state).build();
        // todo 这里还可以将处理解析数据

        return analysisResultDTO;
    }

    public static class Builder {
        private String fileId;
        private String name;
        private int type;
        private Language language;
        private String filePath;
        private String desc;
        private int state;
        private String graphPath;
        private String dataPath;
        private ArrayList<AbstractProgramGraph> graphData;

        public Builder(String fileId, int type, String filePath, String fileName) {
            this.fileId = fileId;
            this.type = type;
            this.filePath = filePath;

            // name 默认为文件名+fileId
            // 若用户有指定则通过 set 方法设置
            this.name = fileName;
            this.state = 1;
        }

        public UploadFile build() {
            return new UploadFile(this);
        }

        public String getFileId() {
            return fileId;
        }

        public Builder setFileId(String fileId) {
            this.fileId = fileId;
            return this;
        }

        public String getName() {
            return name;
        }

        public Builder setName(String name) {
            if (name.isBlank()) return this;
            this.name = name;
            return this;
        }

        public int getType() {
            return type;
        }

        public Builder setType(int type) {
            this.type = type;
            return this;
        }

        public Language getLanguage() {
            return language;
        }

        public Builder setLanguage(Language language) {
            this.language = language;
            return this;
        }

        public String getFilePath() {
            return filePath;
        }

        public Builder setFilePath(String filePath) {
            this.filePath = filePath;
            return this;
        }

        public String getDesc() {
            return desc;
        }

        public Builder setDesc(String desc) {
            this.desc = desc;
            return this;
        }

        public int getState() {
            return state;
        }

        public Builder setState(int state) {
            this.state = state;
            return this;
        }

        public String getGraphPath() {
            return graphPath;
        }

        public Builder setGraphPath(String graphPath) {
            this.graphPath = graphPath;
            return this;
        }

        public String getDataPath() {
            return dataPath;
        }

        public Builder setDataPath(String dataPath) {
            this.dataPath = dataPath;
            return this;
        }

        public ArrayList<AbstractProgramGraph> getGraphData() {
            return graphData;
        }

        public Builder setGraphData(ArrayList<AbstractProgramGraph> graphData) {
            this.graphData = graphData;
            return this;
        }

    }
}
